<?php

/**
 * gpa_new Description
 * @package
 * @name controller_tools
 * @filesource controller_tools.php
 * @author Meta Sanjaya
 */

class model_tools extends Model {
    public function  __construct() {
        parent::__construct();
    }

    public function searchTools($param=array()) {
        $cat = (isset($param['cat'])?$param['cat']:'');
        if(isset($param['tool'])) {
            $tool1 = "'%".$param['tool']."'";
            $tool2 = "'%".$param['tool']."%'";
            $tool3 = "'".$param['tool']."%'";
        } else {
            $tool1 = "''";
            $tool1 = "''";
            $tool2 = "''";
        }
        
        return $this->db->query("SELECT DISTINCT a.tools, (SELECT COUNT(tool_id) FROM #_users_tools b WHERE b.tool_id = a.tool_id) AS jumlah FROM #_users_tools a WHERE a.category = '$cat' AND ( a.tools LIKE '%s' OR a.tools LIKE '%s' OR a.tools LIKE '%s' ) ORDER BY jumlah DESC", $tool1, $tool2, $tool3)->rows;
        //die($this->db->getQuery());
    }

    public function addTool($cat, $tool) {
        $uid = User::getUserId();
        $cat = $this->db->escape($cat);
        $tool = $this->db->escape($tool);
        
        $this->db->query("INSERT INTO #_users_tools SET user_id = '$uid', category = '$cat', tools = '$tool'");
    }

    public function delTool($tid) {
        $this->db->query("DELETE FROM #_users_tools WHERE tool_id = '$tid'");
    }
}

?>
